package com.richard.cloud;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import javax.annotation.Resource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.richard.cloud.entity.Account;
import com.richard.cloud.repo.AccountRepo;
import com.richard.cloud.service.AccountService;
import com.richard.springcloud.base.ComnResult;

import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class BootSeataAccountService9006 {
	public static void main(String[] args) {
		SpringApplication.run(BootSeataAccountService9006.class, args);
	}
}

@Slf4j
@RestController
class TestCtrller {
	@Resource
	private AccountService service;
	@Resource
	private AccountRepo repo;
	
	@GetMapping("/hello")
	public String hello() {
		return "account-service " +LocalDateTime.now().toString();
	}
	
	@PutMapping("/account/charge")
	public ComnResult<?> charge(@RequestParam Long custId, @RequestParam BigDecimal amount){
		log.info(">> 账户扣款开始");
//		service.charge(custId, amount);
		Account entity = repo.findByCustId(custId);
		entity.setBalance(entity.getBalance().subtract(amount));
		repo.saveAndFlush(entity);
		//int i = 1/0;
		log.info("<< 账户扣款结束");
		return ComnResult.succ();
	}
}